/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.adapter;

import static android.graphics.Typeface.BOLD;
import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.bean.ProductItemBean;
import com.dataoke.shoppingguide.app773026.util.GlideUtils;
import com.dataoke.shoppingguide.app773026.util.ParseUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> {

    private List<ProductItemBean> mList = new ArrayList<>();
    private Fragment fragment;
    private Context context;

    private OnClickListener onClickListener;

    private OnItemClickListener onItemClickListener;

    public ProductListAdapter(Fragment fragment) {
        this.fragment = fragment;
        initListener();
    }
    public ProductListAdapter(Context context) {
        this.context = context;
        initListener();
    }
    public interface OnItemClickListener {
        void onItemClick(int position, ProductItemBean itemBean);
    }
    /**
     * 可以共用同一个listener，相对高效
     */
    public static abstract class OnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder) v.getTag();
            onClick(holder.getAdapterPosition());
        }

        public abstract void onClick(int position);
    }

    /**
     * 初始化listener
     */
    private void initListener() {
        onClickListener = new OnClickListener() {
            @Override
            public void onClick(int position) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(position, mList.get(position));
            }
        };

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_haodanku_product_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ProductItemBean contentBean = mList.get(position);

        holder.title.setText(contentBean.getItemShotTitle());
        holder.endPrice.setText("现价" + contentBean.getItemEndPrice());
        if(fragment != null) {
            GlideUtils.loadImgCrop(fragment, contentBean.getItemPic(), holder.pic);
        } else {
            GlideUtils.loadImgCrop(context, contentBean.getItemPic(), holder.pic);
        }

        if(position == 1) {
            ViewGroup.LayoutParams layoutParams = holder.pic.getLayoutParams();
//            layoutParams.height = mHeights.get(position);
            layoutParams.height = (int) (480 + Math.random() * 100);
            holder.pic.setLayoutParams(layoutParams);

        }
        initEndPrice(holder.endPrice, contentBean);
        holder.tvItemSale.setText("已售" + ParseUtils.getItemSale(Long.parseLong(contentBean.getItemSale())) + "件");

        List<String> labels = contentBean.getLabel();
        if(labels != null) {
            if(!labels.isEmpty()) {
                holder.mSpec.setText(labels.get(0));
                holder.mSpec.setVisibility(View.VISIBLE);
            }
            if(labels.size() > 1) {
                holder.mBrand.setText(labels.get(1));
                holder.mBrand.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        ImageView pic;
        TextView endPrice;
        ConstraintLayout layout;
        TextView tvItemSale;
        TextView mSpec;
        TextView mBrand;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_title);
            pic = itemView.findViewById(R.id.iv_pic);
            endPrice = itemView.findViewById(R.id.tv_end_price);
            tvItemSale = itemView.findViewById(R.id.tv_item_sale);
            mSpec = itemView.findViewById(R.id.tv_spec);
            mBrand = itemView.findViewById(R.id.tv_brand);
            layout = itemView.findViewById(R.id.cl_product_item);
            layout.setTag(this);
            layout.setOnClickListener(onClickListener);
        }
    }
    public void addData(List<ProductItemBean> contentBean) {
        mList.addAll(contentBean);
        notifyDataSetChanged();
    }
    public void setData(List<ProductItemBean> contentBean) {
        mList.clear();
        mList.addAll(contentBean);
        notifyDataSetChanged();
    }

    private void initEndPrice(TextView tvEndPrice, ProductItemBean bean) {
        String priceTxt = "￥" + bean.getItemEndPrice() + " ￥" + bean.getItemPrice();
        SpannableStringBuilder builder = new SpannableStringBuilder(priceTxt);

        //ForegroundColorSpan 为文字前景色，BackgroundColorSpan为文字背景色
        ForegroundColorSpan graySpan = new ForegroundColorSpan(Color.GRAY);
        StrikethroughSpan stSpan = new StrikethroughSpan();  //设置删除线样式
        StyleSpan styleSpan = new StyleSpan(BOLD);


        int index = priceTxt.indexOf(" ");

        builder.setSpan(new AbsoluteSizeSpan(15, true), 0, index, SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(styleSpan, 0, index, SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(stSpan, index + 1, priceTxt.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(graySpan, index + 1, priceTxt.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
        tvEndPrice.setText(builder);

    }

}
